﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimhoppKjellsFavoriterVT13
{
    public class PrintContestants : System.Drawing.Printing.PrintDocument
    {
        const string format = "{0, 5} {1, 50} {2, 95} {3, 140}";
        private int pageCounter;
        private List<Contestant> contestantList;
        
        public PrintContestants(List<Contestant> cList) : base()
        {
            contestantList = cList;
        }

        public void printingContestants()
        {
            pageCounter = 0;
            
            //The correct version, the other one is just for testing
            //System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
            System.Windows.Forms.PrintPreviewDialog dialog = new PrintPreviewDialog();

            this.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printOnePage);
            dialog.Document = this;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Print();
            }
        }

        private void printOnePage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        {
            Font HeadFont = new Font(FontFamily.GenericMonospace, 18, FontStyle.Underline);
            Font hFont = HeadFont;
            Font font = new Font(FontFamily.GenericMonospace, 12);
            Font printFont = font;
            Font InfoFont = new Font(FontFamily.GenericMonospace, 14, FontStyle.Bold);
            Font iFont = InfoFont;
            SolidBrush aBrush = new SolidBrush(Color.Black);
            SolidBrush newBrush = new SolidBrush(Color.DarkRed);

            int linesPerPage = (int)(ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics));
            float posY = 0;
            int row = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            float bottomMargin = ev.MarginBounds.Bottom;
            float docWidth = ev.MarginBounds.Width;
            float rightMargin = ev.MarginBounds.Right;

            string line = null;

            row = 1;
            posY = topMargin + row * printFont.GetHeight(ev.Graphics);
            ev.Graphics.DrawString("Contestants", hFont, aBrush, (docWidth + leftMargin) / 2 - 20, posY - 10, new StringFormat());

            row = 2;
            ev.Graphics.DrawString("ID", iFont, newBrush, leftMargin + 30, posY + 20, new StringFormat());
            ev.Graphics.DrawString("Name", iFont, newBrush, rightMargin - 100, posY + 20, new StringFormat());

            row = 3;
            while (row < (linesPerPage - 6))
            {
                line = null;

                int i = pageCounter * (linesPerPage - 6) + (row - 3);
                if (i >= contestantList.Count)
                    break;

                Contestant contestant = contestantList[i];
                line = string.Format(format, contestant.ID, contestant.name,
                    contestant.nationality, contestant.getTotalScore());

                posY = topMargin + row * printFont.GetHeight(ev.Graphics);
                ev.Graphics.DrawString(line, printFont, aBrush, leftMargin, posY, new StringFormat());
                row++;
            
            }

            row++;
            posY = topMargin + row * printFont.GetHeight(ev.Graphics);
            ev.Graphics.DrawString((pageCounter + 1).ToString(), printFont, aBrush, docWidth, bottomMargin - 10, new StringFormat());

            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
            aBrush.Dispose();


            this.pageCounter++;
        }


    }
}
